package cn.lf.android.lawfirm.view.pickerview;

import java.util.ArrayList;


import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import cn.lf.android.lawfirm.R;

/**
 * 选项选择器，可支持一二三级联动选择
 * 
 * @author Sai
 *
 */
public class OptionsPopupWindow extends PopupWindow implements OnClickListener {
	private View rootView; // 总的布局
	WheelOptions wheelOptions;
	private View btnSubmit, btnCancel, btnSubmitBottom;
	private TextView tvTitle;
	private View groupTop;
	private OnOptionsSelectListener optionsSelectListener;
	private static final String TAG_SUBMIT = "submit";
	private static final String TAG_CANCEL = "cancel";

	/**
	 * @JingYang 实现三级PickerView的异步联动功能
	 */
	private MyExtendListener mMyExtendListener;

	/********************************************/
	/********************************************/
	/**
	 * @author JingYang 项目中收货地址采用省市区三级列表，如果某一项文字太长，会在滑动过程中产生重叠现象。
	 *         这个WheelView是用StaticLayout来控制text的绘制的，只能自动换行。
	 *         为了避免这个问题只能把文字超过一定长度的内容用“...”截断
	 *         setSingleLine()需要和setMaxTextItem()配合使用
	 */
	/**
	 * 设置是否单行显示
	 * 
	 * @param single
	 */
	public void setSingleLine(boolean single) {
		wheelOptions.setSingleLine(single);
	}

	/**
	 * 设置超过多少位的文字采取“...”截断 项目中需要这个功能的只有选择省市县地址，传7就好
	 * 
	 * @param num
	 */
	public void setMaxTextItem(int num) {
		wheelOptions.setMaxTextItem(num);
	}

	/**
	 * 设置文字大小
	 * 
	 * @param size
	 */
	public void setTextSize(int size) {
		wheelOptions.setTextSize(size);
	}

	/**
	 * 设置第一个选项宽度
	 * 
	 * @param size
	 */
//	public void setWheelOptions1Width(int size) {
//		wheelOptions.setWheelOptions1Width(size);
//	}

	/********************************************/

	public OptionsPopupWindow(Context context) {
		super(context);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// this.setBackgroundDrawable(new BitmapDrawable());//
		// 这样设置才能点击屏幕外dismiss窗口
		this.setBackgroundDrawable(new ColorDrawable(Color.argb(0x55, 0, 0, 0)));// 这样设置才能点击屏幕外dismiss窗口
		this.setOutsideTouchable(true);
		this.setAnimationStyle(R.style.PickerViewAnimStyle);

		LayoutInflater mLayoutInflater = LayoutInflater.from(context);
		rootView = mLayoutInflater.inflate(R.layout.pickerview_options, null);
		// -----确定和取消按钮
		btnSubmit = rootView.findViewById(R.id.btnSubmit);
		btnSubmit.setTag(TAG_SUBMIT);
		btnSubmitBottom = rootView.findViewById(R.id.btnSubmit_bottom);
		btnSubmitBottom.setTag(TAG_SUBMIT);
		btnCancel = rootView.findViewById(R.id.btnCancel);
		btnCancel.setTag(TAG_CANCEL);
		btnSubmit.setOnClickListener(this);
		btnSubmitBottom.setOnClickListener(this);
		btnCancel.setOnClickListener(this);

		tvTitle = (TextView) rootView.findViewById(R.id.tv_title);
		groupTop = rootView.findViewById(R.id.group_top);
		// ----转轮
		final View optionspicker = rootView.findViewById(R.id.optionspicker);
		optionspicker.setVisibility(View.VISIBLE);
		ScreenInfo screenInfo = new ScreenInfo((Activity) context);
		wheelOptions = new WheelOptions(optionspicker);

		wheelOptions.screenheight = screenInfo.getHeight();

		setContentView(rootView);
	}
	public OptionsPopupWindow(Context context,int i) {
		super(context);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// this.setBackgroundDrawable(new BitmapDrawable());//
		// 这样设置才能点击屏幕外dismiss窗口
		this.setBackgroundDrawable(new ColorDrawable(Color.argb(0x55, 0, 0, 0)));// 这样设置才能点击屏幕外dismiss窗口
		this.setOutsideTouchable(true);
		this.setAnimationStyle(R.style.PickerViewAnimStyle);

		LayoutInflater mLayoutInflater = LayoutInflater.from(context);
		rootView = mLayoutInflater.inflate(R.layout.pickerview_options, null);
		// -----确定和取消按钮
		btnSubmit = rootView.findViewById(R.id.btnSubmit);
		btnSubmit.setTag(TAG_SUBMIT);
		btnSubmitBottom = rootView.findViewById(R.id.btnSubmit_bottom);
		btnSubmitBottom.setTag(TAG_SUBMIT);
		btnCancel = rootView.findViewById(R.id.btnCancel);
		btnCancel.setTag(TAG_CANCEL);
		btnSubmit.setOnClickListener(this);
		btnSubmitBottom.setOnClickListener(this);
		btnCancel.setOnClickListener(this);

		tvTitle = (TextView) rootView.findViewById(R.id.tv_title);
		groupTop = rootView.findViewById(R.id.group_top);
		// ----转轮
		final View optionspicker = rootView.findViewById(R.id.optionspicker2);
		optionspicker.setVisibility(View.VISIBLE);
		ScreenInfo screenInfo = new ScreenInfo((Activity) context);
		wheelOptions = new WheelOptions(optionspicker);

		wheelOptions.screenheight = screenInfo.getHeight();

		setContentView(rootView);
	}
	/**
	 * @JingYang 实现三级PickerView的异步联动功能
	 */
	/**
	 * 设置监听事件，指定该listener后，PickerView的list在数据发生onChange的时候通知外部进行异步加载sub list数据
	 * 
	 * @param listener
	 */
	public void setMyExtendListener(MyExtendListener listener) {
		mMyExtendListener = listener;
	}

	/**
	 * 外部进行异步加载sub list完成后，通过该callback通知wheelOptions列表发生变化
	 * 
	 * @return
	 */
	public MyExtendAsyncCallback getExtendAsyncCallback() {
		return wheelOptions;
	}

	/********************************************/
	// public void setPicker_smallone(ArrayList<String> options1Items,
	// ArrayList<ArrayList<String>> options2Items, boolean linkage) {
	// setWheelOptions1Width(2);
	// wheelOptions.setPicker(options1Items, options2Items, options3Items,
	// linkage);
	// }

	public void setPicker(ArrayList<String> optionsItems) {
		wheelOptions.setPicker(optionsItems, null, null, false);
	}
	public void setPicker(ArrayList<String> optionsItems,ArrayList<String> optionsItems2,ArrayList<String> optionsItems3) {
		wheelOptions.setPicker(optionsItems, optionsItems2, optionsItems3);
	}
	public void setPicker(ArrayList<String> options1Items,
			ArrayList<ArrayList<String>> options2Items, boolean linkage) {
		wheelOptions.setPicker(options1Items, options2Items, null, linkage);
	}

	public void setPicker(ArrayList<String> options1Items,
			ArrayList<ArrayList<String>> options2Items,
			ArrayList<ArrayList<ArrayList<String>>> options3Items,
			boolean linkage) {
		wheelOptions.setPicker(options1Items, options2Items, options3Items,
				linkage);

		/**
		 * @JingYang 实现三级PickerView的异步联动功能
		 */
		wheelOptions.setMyExtendListener(mMyExtendListener);
		/********************************************/
	}

	/**
	 * 设置选中的item位置
	 * 
	 * @param option1
	 */
	public void setSelectOptions(int option1) {
		wheelOptions.setCurrentItems(option1, 0, 0);
	}

	/**
	 * 设置选中的item位置
	 * 
	 * @param option1
	 * @param option2
	 */
	public void setSelectOptions(int option1, int option2) {
		wheelOptions.setCurrentItems(option1, option2, 0);
	}

	/**
	 * 设置选中的item位置
	 * 
	 * @param option1
	 * @param option2
	 * @param option3
	 */
	public void setSelectOptions(int option1, int option2, int option3) {
		wheelOptions.setCurrentItems(option1, option2, option3);
	}

	/**
	 * 设置选项的单位
	 * 
	 * @param label1
	 */
	public void setLabels(String label1) {
		wheelOptions.setLabels(label1, null, null);
	}

	/**
	 * 设置选项的单位
	 * 
	 * @param label1
	 * @param label2
	 */
	public void setLabels(String label1, String label2) {
		wheelOptions.setLabels(label1, label2, null);
	}

	/**
	 * 设置选项的单位
	 * 
	 * @param label1
	 * @param label2
	 * @param label3
	 */
	public void setLabels(String label1, String label2, String label3) {
		wheelOptions.setLabels(label1, label2, label3);
	}

	/**
	 * 设置是否循环滚动
	 * 
	 * @param cyclic
	 */
	public void setCyclic(boolean cyclic) {
		wheelOptions.setCyclic(cyclic);
	}

	@Override
	public void onClick(View v) {
		String tag = (String) v.getTag();
		if (tag.equals(TAG_CANCEL)) {
			dismiss();
			return;
		} else {
			if (optionsSelectListener != null) {
				int[] optionsCurrentItems = wheelOptions.getCurrentItems();
				optionsSelectListener.onOptionsSelect(optionsCurrentItems[0],
						optionsCurrentItems[1], optionsCurrentItems[2]);
			}
			dismiss();
			return;
		}
	}

	public interface OnOptionsSelectListener {
		public void onOptionsSelect(int options1, int option2, int options3);
	}

	public void setOnoptionsSelectListener(
			OnOptionsSelectListener optionsSelectListener) {
		this.optionsSelectListener = optionsSelectListener;
	}

	public void setTitle(String title) {
		if (title != null) {
			tvTitle.setText(title);
			tvTitle.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 项目需求，选择省市县列表将确定按钮置于底部，顶部按钮全部隐藏
	 */
	public void applyBottomStyle() {
		btnSubmitBottom.setVisibility(View.VISIBLE);
		groupTop.setVisibility(View.GONE);
	}
}
